<?php

/**
 * AmplifyCMS
 *
 * This file is part of AmplifyCMS.
 *
 * AmplifyCMS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * AmplifyCMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @copyright Copyright 2008 Rodrigo Rutkoski Rodrigues
 */

/**
 *
 * @author Rutkoski
 *
 */
class OptionsController extends AmpActionController
{

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/controller/Controller#action_index()
   */
  protected function action_index($page = 'general')
  {
    if (method_exists($this, 'page_' . $page)) {
      $this->response->set('formTitle', __('Options') . ' - ' . Inflector::humanize($page));
      $this->setTemplate('options_' . $page); //.'_view');
      return call_user_func(array(
        $this, 'page_' . $page
      ));
    }
    
    return $this->response->redirect('/404');
  }

  /**
   * General Options.
   */
  protected function page_general()
  {
    if ($this->form() === true) {
      $this->response->notice(__('Data saved'));
      return $this->response->redirect('/options/general');
    }
  }

  /**
   * Mail Options
   */
  protected function page_mail()
  {
    $this->response->set('mailEngineOptions', array(
      'MAIL' => 'mail', 'SMTP' => 'smtp'
    ));
    $this->response->set('mailAuthOptions', array(
      'TRUE' => __('yes'), 'FALSE' => __('no')
    ));
    
    if ($this->form() === true) {
      $this->response->notice(__('Data saved'));
      return $this->response->redirect('/options/mail');
    }
  }

  /**
   * Modules
   */
  protected function page_modules()
  {
    $FoundModules = array();
    
    $modules = Modules::findAll($this->config->get('home_dir'));
    foreach ($modules as $name => $path) {
      $FoundModules[$path] = Inflector::humanize($name);
    }
    
    $this->response->set('FoundModules', $FoundModules);
    $this->response->set('ActiveModules', Options::value('amp_modules'));
    
    if ($this->form) {
      Options::update('amp_modules', $this->form->get('amp_modules', array()));
      $this->response->notice(__('Data saved'));
      $this->response->redirect('/options/modules');
    }
  }

  /**
   *
   * @return boolean True if data saved sucessfully.
   */
  protected function form()
  {
    if ($this->form) {
      foreach ($this->form->getAll() as $name => $value) {
        Options::update($name, $value);
      }
      
      return true;
    }
  }

}

?>